import { base_url } from './config.js';
import { getToken } from '../util/tool.js';

export const upload = {
    // 创建头像上传节点
	async createAvatarNode() {
		let jwt = getToken();
		return new Promise((resolve, reject) => {
			uni.request({
				url: base_url + '/upload/createAvatarNode',
				method: 'POST',
				header: {
					'content-type': 'application/json',
					'Authorization': jwt
				},
				success(res) {
					console.log('【 Response in request createAvatarNode 】', res);
					resolve(res.data);
				},
				fail(err) {
					console.log('【 Error in request createAvatarNode 】', err);
					reject({ success: false });
				}
			});
		});
	},
    // 创建图片上传节点
    async createImgNode() {
		let jwt = getToken();
		return new Promise((resolve, reject) => {
			uni.request({
				url: base_url + '/upload/createImgNode',
				method: 'POST',
				header: {
					'content-type': 'application/json',
					'Authorization': jwt
				},
				success(res) {
					console.log('【 Response in request createImgNode 】', res);
					resolve(res.data);
				},
				fail(err) {
					console.log('【 Error in request createImgNode 】', err);
					reject({ success: false });
				}
			});
		});
	},
    // 获取头像上传地址
    async getUploadAvatarUrl(avatarId) {
		let jwt = getToken();
		return new Promise((resolve, reject) => {
			uni.request({
				url: base_url + '/upload/getUploadAvatarUrl',
				method: 'POST',
				header: {
					'content-type': 'application/json',
					'Authorization': jwt
				},
                data: { avatarId },
				success(res) {
					console.log('【 Response in request getUploadAvatarUrl 】', res);
					resolve(res.data);
				},
				fail(err) {
					console.log('【 Error in request getUploadAvatarUrl 】', err);
					reject({ success: false });
				}
			});
		});
	},
    // 获取图片上传地址
    async getUploadImgUrl(imgId) {
		let jwt = getToken();
		return new Promise((resolve, reject) => {
			uni.request({
				url: base_url + '/upload/getUploadImgUrl',
				method: 'POST',
				header: {
					'content-type': 'application/json',
					'Authorization': jwt
				},
                data: { imgId },
				success(res) {
					console.log('【 Response in request getUploadImgUrl 】', res);
					resolve(res.data);
				},
				fail(err) {
					console.log('【 Error in request getUploadImgUrl 】', err);
					reject({ success: false });
				}
			});
		});
	},
    // 确认头像上传完成接口
    async avatarUploadOK(avatarId) {
		let jwt = getToken();
		return new Promise((resolve, reject) => {
			uni.request({
				url: base_url + '/upload/avatarUploadOK',
				method: 'POST',
				header: {
					'content-type': 'application/json',
					'Authorization': jwt
				},
                data: { avatarId },
				success(res) {
					console.log('【 Response in request avatarUploadOK 】', res);
					resolve(res.data);
				},
				fail(err) {
					console.log('【 Error in request avatarUploadOK 】', err);
					reject({ success: false });
				}
			});
		});
	},
    // 确认图片上传完成接口
    async imgUploadOK(imgId) {
		let jwt = getToken();
		return new Promise((resolve, reject) => {
			uni.request({
				url: base_url + '/upload/imgUploadOK',
				method: 'POST',
				header: {
					'content-type': 'application/json',
					'Authorization': jwt
				},
                data: { imgId },
				success(res) {
					console.log('【 Response in request imgUploadOK 】', res);
					resolve(res.data);
				},
				fail(err) {
					console.log('【 Error in request imgUploadOK 】', err);
					reject({ success: false });
				}
			});
		});
	},
    // 获取图片预签名URL
	async getPublicImgUrl(imgId) {
		let jwt = getToken();
		return new Promise((resolve, reject) => {
			uni.request({
				url: base_url + '/upload/getPublicImgUrl?id=' + imgId,
				method: 'GET',
				header: {
					'content-type': 'application/json',
					'Authorization': jwt
				},
				success(res) {
					console.log('【 Response in request getPublicImgUrl 】', res);
					resolve(res.data);
				},
				fail(err) {
					console.log('【 Error in request getPublicImgUrl 】', err);
					reject({ success: false });
				}
			});
		});
	}

};